//package yh.thread.config;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.AsyncConfigurer;
//import org.springframework.scheduling.annotation.EnableAsync;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//
//import java.lang.reflect.Method;
//import java.util.Arrays;
//import java.util.concurrent.ThreadPoolExecutor;
//
///**
// * 线程池配置类
// * @author: zyh
// * @date: 2024/11/1 10:40
// **/
//@Configuration
//@EnableAsync
//public class ThreadPoolConfig implements AsyncConfigurer {
//
//    private static Logger logger = LoggerFactory.getLogger(ThreadPoolConfig.class);
//
//    /**
//     * 异步线程池
//     * @return
//     */
//    @Bean(destroyMethod = "shutdown",name = "asyncTaskPool")
//    public ThreadPoolTaskExecutor getAsyncExecutor(){
//        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
//        //核心线程数
//        threadPoolTaskExecutor.setCorePoolSize(2);
//        //最大线程数
//        threadPoolTaskExecutor.setMaxPoolSize(5);
//        //任务队列大小
//        threadPoolTaskExecutor.setQueueCapacity(100);
//        //存活时间
//        threadPoolTaskExecutor.setKeepAliveSeconds(60);
//        //线程名称前缀
//        threadPoolTaskExecutor.setThreadNamePrefix("async-pool-");
//        //交给提交任务的线程来执行
//        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        //实例化
//        threadPoolTaskExecutor.initialize();
//        //项目启动线程初始化日志打印
//        logger.info("ThreadPoolTaskExecutor initialized with corePoolSize: {}, maxPoolSize: {}, queueCapacity: {}",
//                threadPoolTaskExecutor.getCorePoolSize(), threadPoolTaskExecutor.getMaxPoolSize(), threadPoolTaskExecutor.getQueueCapacity());
//        return threadPoolTaskExecutor;
//    }
//
//
//    /**
//     * 自定义异常处理器
//     * @return
//     */
//    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler(){
//         return  new MyAsyncUncaughtException();
//   }
//
//
//
//    class MyAsyncUncaughtException implements AsyncUncaughtExceptionHandler{
//        @Override
//        public void handleUncaughtException(Throwable ex, Method method, Object... params) {
//            logger.error("myAsync Exception " + ex.getMessage()+ " method " + method.getName() + " parameter " + Arrays.toString(params));
//        }
//    }
//
//}
